package com.wonders.hisdoctor.station.util;

import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * @author: lidongyang
 * @date: 2020/12/28
 * description
 */
public class CountAgeUtil {
    public static String countAge(String from){
        String age = "";
        int year = 0;
        int month = 0;
        int day = 0;
        from = from.substring(0, 10).replace("-", "");
        int birthYear = Integer.parseInt(from.substring(0, 4));
        int birthMonth = Integer.parseInt(from.substring(4, 6))-1;
        int birthDay = Integer.parseInt(from.substring(6, 8));
        Calendar birthday = new GregorianCalendar(birthYear, birthMonth, birthDay);
        if(from == null){
            age = age + 0;
        } else {
            Calendar c1 =  new GregorianCalendar(birthYear, birthMonth, birthDay);
            Calendar c2 = Calendar.getInstance();
            c2.setTime(new Date());
            if (c1.after(c2)) {
                throw new IllegalArgumentException("生日不能超过当前日期");
            }
            int from_year = c1.get(Calendar.YEAR);
            int from_month = c1.get(Calendar.MONTH)+1;
            int from_day = c1.get(Calendar.DAY_OF_MONTH);
            int MaxDayOfMonth = c1.getActualMaximum(Calendar.DAY_OF_MONTH);
            int to_year = c2.get(Calendar.YEAR);
            int to_month = c2.get(Calendar.MONTH)+1;
            int to_day = c2.get(Calendar.DAY_OF_MONTH);
            year = to_year - from_year;
            if(to_month - from_month>0){
                month = to_month -from_month;
                if(to_day - from_day < 0){
                    month = month - 1;
                    day = to_day -from_day + MaxDayOfMonth;
                } else {
                    day= to_day -from_day;
                }
            }else if(to_month - from_month==0){
                if(to_day - from_day < 0){
                    --year;
                    month = 11;
                    day = to_day -from_day + MaxDayOfMonth;
                } else {
                    day= to_day -from_day;
                }
            }else if(to_month-from_month<0){
                --year;
                month = to_month -from_month+12;
                if(to_day - from_day < 0){
                    month = month - 1;
                    day = to_day -from_day + MaxDayOfMonth;
                } else {
                    day= to_day -from_day;
                }

            }
            //年龄>=3岁显示岁
            if(year>=3){
                age=age+year+"岁";
            }
            //年龄>=1岁小于3岁显示岁+月
            if(year>=1&&year<3){
                age=age + year + "岁"+month + "个月";
            }
            if(year<1){
                //年龄<1岁>=1月显示月+天
              if(month>0){
                age=age+month + "个月" + day + "天";
              }
              //年龄<1月展示天
              if(month<=0){
                  age=age+ day + "天";
              }            }
           // age=age + year + "岁"+month + "个月" + day + "天";
        }
        return age;
    }
}
